package com.icinfo.cloud.provider.common.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.framework.common.web.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 描述：调用处罚主项目http工具类
 *
 * @Author zhengqiang
 * @Date 2022/7/4 10:33
 **/
@Component
public class PunishMainClientUtil {

    private static String url;

    @Value("${tzService.url}")
    private void setUrl(String url) {
        this.url = url;
    }

    /**
     * 调用处罚主项目post请求
     *
     * @param uri 请求uri
     * @param param 请求参数对象
     * @param <T>
     * @return
     */
    public static <T> Result httpPost(String uri, T param) {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String paramStr = JSONObject.toJSONString(param);
        String response = HttpUtil.createPost(url.concat(uri)).addHeaders(headers).body(paramStr).execute().body();
        Result result = JSONObject.parseObject(response, Result.class);
        return result;
    }

    /**
     *  调用处罚主项目post请求
     *
     * @param uri 请求uri
     * @param bodyParam 请求参数对象
     * @param urlParam url拼接参数
     * @param <T>
     * @return
     */
    public static <T> Result httpPost(String uri, T bodyParam, Map<String, Object> urlParam) {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String paramStr = JSONObject.toJSONString(bodyParam);
        String response = HttpUtil.createPost(url.concat(uri)).addHeaders(headers).form(urlParam).body(paramStr).execute().body();
        Result result = JSONObject.parseObject(response, Result.class);
        return result;
    }

    /**
     * 调用处罚主项目get请求
     *
     * @param uri 请求uri
     * @param urlParam 请求参数
     * @param <T>
     * @return
     */
    public static <T> Result httpGet(String uri, Map<String, Object> urlParam) {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String response = HttpUtil.createGet(url.concat(uri)).addHeaders(headers).form(urlParam).execute().body();
        Result result = JSONObject.parseObject(response, Result.class);
        return result;
    }

}
